#include "coder.h"

void bc_encode(unsigned int id,FILE* out)
{
  unsigned int v;
  unsigned char tmp; 

  v = id;
  while( v >= RADIX )
  {
    tmp = v % RADIX;
    fwrite(&tmp, sizeof(char), 1, out);
    v = v/RADIX - 1;
  }

  tmp = RADIX + v;
  fwrite(&tmp, sizeof(char), 1, out);
}


unsigned int bc_decode(FILE* in)
{
  unsigned int v, p;
  unsigned char x;
  v = 0;
  p = 1;
  fread(&x, sizeof(char), 1, in); 

  while( (unsigned int)x < RADIX )
  {
    v = v + (x+1) * p;
    p = p * RADIX;
    fread(&x, sizeof(char), 1, in); 
  }

  v = v + (x + 1 - RADIX) * p;
  return (v-1);
}
